package org.spidermesh.meshlog.core.persist;

import com.alibaba.fastjson.JSON;
import org.spidermesh.meshlog.util.QueryUtil;
import org.spidermesh.meshlog.core.persist.interfaces.DefaultMeshLogDao;
import org.spidermesh.meshlog.core.persist.interfaces.Persist;
import org.spidermesh.meshlog.model.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @PackageName:org.spidermesh.meshlog.core.persist.interfaces
 * @ClassName:DefaultPersist
 * @Description:
 * @Author:hw
 * @Date:2020-12-9 17:02
 */
@Component
@ConditionalOnBean(value = QueryUtil.class)
public class DefaultPersist implements Persist{
    @Autowired
    private DefaultMeshLogDao meshLogDao;
    @Autowired
    QueryUtil queryUtil;

    @Override
    public void writeLog(LogInfo logInfo){
        System.out.println(JSON.toJSONString(logInfo));
        meshLogDao.save(logInfo);
    }

    @Override
    public int writeLogBatch(List<LogInfo> logInfoList) {
        return meshLogDao.saveAll(logInfoList).size();
    }

    @Override
    public List<LogInfo> findLog(LogInfo logParam){
        return queryUtil.paramBuilder(logParam).query();
    }

    @Override
    public int removeLog(String param,String lower,String upper,Boolean reverse){
        return queryUtil.rangeBuilder(param,lower,upper,reverse).update();
    }
    @Override
    public void removeLogitem(LogInfo logInfo){
        meshLogDao.delete(logInfo);
    }
}
